package com.cndw.kungfu.business;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.event.GamePowerNoticeHandler;
import com.cndw.kungfu.event.GamePowerHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.business.IConstantsConfigManger;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessPower {
	private static Log loger = LogUtil.getMain();
	@Autowired
	private IConstantsConfigManger configManger;

	private static ScheduledFuture<?> noticeTask;

	private static ScheduledFuture<?> closePowerTask;

	private static ScheduledFuture<?> openTask;

	@UnAuth
	@GmCommand
	public ResponseMessage closeCancel(Session session, String sign) {
		closeCancel();
		return ResponseMessage.getSuccMessage(true);
	}

	private boolean closeCancel() {
		if (ConstantsSystem.getPowerCloseTime() == 0) {
			return false;
		}
		if (closePowerTask != null) {
			closePowerTask.cancel(false);
		}
		if (noticeTask != null) {
			noticeTask.cancel(false);
		}
		ConstantsSystem.setPowerCloseTime(0);
		GamePowerNoticeHandler.close();
		loger.info("closeCancel");
		return true;
	}

	@UnAuth
	@GmCommand
	public ResponseMessage powerSet(Session session, String sign, String powerOpenTime, String powerCloseTime,String colseMsg) {
		openSet(powerOpenTime);
		closeSet(powerCloseTime,colseMsg);
		return ResponseMessage.getSuccMessage(true);
	}

	private boolean openSet(String timeString) {
		if (timeString == null || timeString.equals("")) {
			if (openTask != null) {
				openTask.cancel(false);
				ConstantsSystem.setPowerOpenTime(0);
				loger.info("open Cancel");
			}
			return false;
		}
		try {
			int powerOpenTime = TimeProcesser.getUnixTimeByString(timeString);
			if (powerOpenTime == ConstantsSystem.getPowerOpenTime()) {
				return true;
			}
			int currentTime = TimeProcesser.getUnixTime();
			if (powerOpenTime < currentTime) {
				return false;
			}
			if (openTask != null) {
				openTask.cancel(false);
			}
			ConstantsSystem.setPowerOpenTime(powerOpenTime);
			int delayTime = powerOpenTime - currentTime;
			GamePowerHandler powerHandler = new GamePowerHandler(true);
			openTask = Context.getTimerServer().scheduleSeconds(powerHandler, delayTime);
			loger.info("open set " + timeString + " delayTime:" + delayTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return true;
	}

	private boolean closeSet(String timeString,String colseMsg) {
		try {
			if (timeString == null || timeString.length() ==0 || timeString.equals("")) {
				closeCancel();
				return true;
			}
			int colseTime = TimeProcesser.getUnixTimeByString(timeString);
			if (colseTime + 300 <= TimeProcesser.getUnixTime()) {// 比当前时间小5分钟认为是非法的
				return false;
			}
			if (colseTime != ConstantsSystem.getPowerCloseTime()) {
				closeCancel();
			} else {
				if (colseMsg != null) {
					GamePowerNoticeHandler.setColseMsg(colseMsg);
				}
				return false;
			}

			if (closePowerTask != null && !closePowerTask.isDone()) {
				return false;
			} else if (noticeTask != null && !noticeTask.isDone()) {
				return false;
			}
			ConstantsSystem.setPowerCloseTime(colseTime);
			int currentTime = TimeProcesser.getUnixTime();
			int delayTime = colseTime - currentTime;
			GamePowerHandler powerHandler = new GamePowerHandler(false);
			closePowerTask = Context.getTimerServer().scheduleSeconds(powerHandler, delayTime);
			loger.info("close set " + timeString);

			if (delayTime >= 60) {
				int initialDelayMinute = delayTime / 60;
				initialDelayMinute = initialDelayMinute >= 15 ? 15 : initialDelayMinute;
				int initDelayTime = delayTime - initialDelayMinute * 60;
				GamePowerNoticeHandler noticeHandler = new GamePowerNoticeHandler();
				GamePowerNoticeHandler.setColseMsg(colseMsg);
				noticeHandler.reset();
				
				
				noticeTask = Context.getTimerServer().scheduleAtFixedRate(noticeHandler, initDelayTime, 30);
				loger.info("close set initDelayTime:" + initDelayTime);
			}

		} catch (Exception e) {
			return false;
		}

		return true;
	}

	@UnAuth
	@GmCommand
	public ResponseMessage powerGet(Session session, String sign) {
		Map<String, Object> map = new HashMap<String, Object>();
		String powerCloseTime = "", powerOpenTime = "";
		if (ConstantsSystem.getPowerOpenTime() > 0) {
			powerOpenTime = TimeProcesser.getDateByUnixTime(ConstantsSystem.getPowerOpenTime());
		}
		if (ConstantsSystem.getPowerCloseTime() > 0) {
			powerCloseTime = TimeProcesser.getDateByUnixTime(ConstantsSystem.getPowerCloseTime());
		}
		map.put("powerCloseTime", powerCloseTime);
		map.put("powerOpenTime", powerOpenTime);
		map.put("powerCloseMsg", GamePowerNoticeHandler.getColseMsg());
		

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage closeImmediately(Session session, String sign) {
		try {
			GamePowerHandler.commShutDown();
		} catch (Exception e) {
			e.printStackTrace();
		}
		configManger.updateConfig("sysClosed", String.valueOf(1));

		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage closeState(Session session, String sign) {
		return ResponseMessage.getSuccMessage(ConstantsSystem.getCloseState());
	}

}
